from multiprocessing import Process, Queue
import time, random, os
from loguru import logger

def print_files(path,q):
    lsdir = os.listdir(path)
    dirs = []
    for i in lsdir:
        sub_path = os.path.join(path, i)
        if os.path.isdir(sub_path):
            dirs.append(i)
    if dirs:
        for i in dirs:
            print_files(os.path.join(path, i),q)
    files = []
    for k in lsdir:
        sub_file= os.path.join(path, k)
        if os.path.isfile(sub_file):
            files.append(k)
    for f in files:
        # print(os.path.join(path, f))
        real_file = os.path.join(path, f)
        if q.qsize() > 39:
            sleep_time = random.randint(1, 3)
            logger.info('当前生产对了元素总数达到39了，消费不过来了，停止一下: {sleep}', sleep=sleep_time)
            time.sleep(sleep_time)
        q.put(real_file)
    q.put(None)  # 发送结束信号
    logger.info('生产者线程已经将目录遍历完成了')


def consumer(q):
    while True:
        res = q.get()
        if res is None: break  # 收到结束信号则结束
        logger.info('拿到的目录路径为：{x}', x=res)


def producer(q):
    path = 'D:\\apache-maven-3.6.1'

    print_files(path,q)


if __name__ == '__main__':
    logger.add("get_image_file.log", format="{time} {level} {message}", filter="", level="INFO")
    logger.info('开始遍历目录，去寻找image图片，并复制到新目录')
    q = Queue(40)
    # 生产者: 获取目录路径
    p1 = Process(target=producer, args=(q,))

    # 消费者：遍历目录路径
    c1 = Process(target=consumer, args=(q,))

    # 开始
    p1.start()
    c1.start()
    logger.info('主进程。。。。')
